﻿using DNFImagePacks2Manager.Helpers;
using System;
using System.Collections.Generic;
using System.IO;

namespace DNFImagePacks2Manager.Models
{
    public class NPKCustomLine(string filename, string? path = null) : NPKCustomBase(filename, path)
    {
        public override List<NPKItemBase> GetNPKItemList()
        {
            List<NPKItemBase> npkList = [];
            string ImageBasePath = LocalStorageHelper.GetImagePath();
            List<string> npkNameList = GetNPKNameList();
            foreach (var npkName in npkNameList)
            {
                NPKItemBase? npk = NPKItemImage.GetImageNPKItem(npkName, ImageBasePath);
                if(npk != null)
                {
                    npkList.Add(npk);
                }
            }
            return npkList;
        }

        public override List<string> GetNPKNameList()
        {
            List<string> npkList = [];
            if (File.Exists(CsmFile))
            {
                try
                {
                    string[] allLines = File.ReadAllLines(CsmFile);
                    foreach (string line in allLines)
                    {
                        if (string.IsNullOrWhiteSpace(line))
                            continue;
                        npkList.Add(line);
                    }
                }
                catch (Exception) { }
            }
            return npkList;
        }

        public bool TransferToJson()
        {
            return CustomHelper.SaveCustomFile(Name, GetNPKItemList());
        }
    }
}
